/**
 *  FlightLoginForm
 */
package com.longtop.leaning.ui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.longtop.leaning.dao.UserDaoFactory;
import com.longtop.leaning.data.AppData;
import com.longtop.leaning.data.FlightUIData;
import com.longtop.leaning.midlet.FlightMIDlet;


public class FlightLoginForm extends Form implements CommandListener {
	private Item _noteItem = null;
	private static TextField _userTextField = null;
	private static TextField _pwTextField = null;
	
	private FlightMIDlet _flightMIDlet = null;
	
	public FlightLoginForm(FlightMIDlet flightMIDlet) {
		super("登入");
		_flightMIDlet = flightMIDlet;
		_userTextField = new TextField ("工号：", "", 15, TextField.ANY);
		_pwTextField = new TextField ("密码：", "", 15, TextField.PASSWORD);
		this.append (_userTextField);
		this.append (_pwTextField);
		this.addCommand(FlightUIData.CMD_EXIT);
		this.addCommand(FlightUIData.CMD_OK);
		this.setCommandListener(this);
	}	
	
	public FlightLoginForm(String arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}
	public FlightLoginForm(String arg0, Item[] arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	public void commandAction(Command cmd, Displayable d) {
		// TODO Auto-generated method stub
		if (cmd == FlightUIData.CMD_OK) {
			String loginid = _userTextField.getString();
			String pw = _pwTextField.getString();
			System.out.println("loginid:"+loginid);
			System.out.println("password:"+pw);
			if(UserDaoFactory.getUserDao().getUser(loginid, pw) != null){
//				User user = new User(loginid, pw);
				//登录成功处理
				if(AppData.isOnLine){
					_flightMIDlet.showFlightMenu();
				}else{
					_flightMIDlet.showFlightMenu();
				}
//				UserDaoFactory.getUserDao().test();
			}else{
				//登录失败处理
				if(_noteItem == null){
					_noteItem = new StringItem ("工号或者密码错误！","");
					this.insert(0, _noteItem);
				}
				_flightMIDlet.showLoginForm();
			}
		}else if(cmd == FlightUIData.CMD_EXIT){
			_flightMIDlet.destroy(false);
		}
	}
}
